package com.zhongxi.guoyuan.OffLineShop.Utils;

import android.os.Environment;

import com.zhongxi.guoyuan.utils.UiUtils;

import java.io.File;

/**
 * Created by wangx on 0016.
 * 统一维护所有的缓存目录
 */
public class FileUitls {

    public static final String ROOT = "com.zhongxi.guoyuan"; //根目录
    public static final String CACHE = "cache"; //缓存数据的目录
    public static final String IMAGE = "image";

    //  /mnt/sdcard/com.zhongxi.guoyuan/cache
    //  /mnt/sdcard/com.zhongxi.guoyuan/image
    //  /mnt/sdcard/com.zhongxi.guoyuan/apk
    //  /data/data/包名/cache/cache
    //  /data/data/包名/cache/image
    //  /data/data/包名/cache/apk
    public static File getDir(String dir) {
        StringBuilder path = new StringBuilder();

        if (isSdAvailable()) {
            path.append(Environment.getExternalStorageDirectory().getAbsolutePath()); //  /mnt/sdcard
            path.append(File.separator); //  /mnt/sdcard/
            path.append(ROOT);
            path.append(File.separator); //  /mnt/sdcard/googleplay/
            path.append(dir);
        } else {
            path.append(UiUtils.getContext().getCacheDir().getAbsolutePath());//   /data/data/cache
            path.append(File.separator);//  /data/data/cache/
            path.append(dir);
        }
        File file = new File(path.toString());
        if (!file.exists()) {
            //创建目录
            file.mkdirs();
        }

        return file;
    }

    /**
     * 判断sd卡是否可用
     *
     * @return
     */
    public static boolean isSdAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }


    /**
     * 获取维护缓存数据的目录
     *
     * @return
     */
    public static File getCacheDir() {
        return getDir(CACHE);
    }


    /**
     * 获取图片缓存目录
     * @return
     */
    public static File getImageDir(){
        return getDir(IMAGE);
    }
}
